package com.chocho.producerserver.service;

import com.chocho.producerserver.bean.Visited;
import com.chocho.producerserver.dao.VisitedDao;
import com.chocho.producerserver.utils.UUIDUtil;
import com.chocho.producerserver.vo.VHost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;

/**
 * Created by chocho on 2022/4/12.
 */
@Service
public class StatisticService {

    @Autowired
    private VisitedDao visitedDao;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserService userService;

    public int getVisitedNum(){
        return visitedDao.count();
    }

    public boolean setVisitedNum(HttpServletRequest request){
        Visited visited = new Visited();
        visited.setVisitedId(UUIDUtil.getUUID());
        visited.setVisitedTime(new Date(System.currentTimeMillis()));
        visited.setUserId("1d822bfd1d3f421cb1ecd6e1e70ec9ba");
        visited.setVisitedUrl(request.getRequestURL().toString());

        return visitedDao.insert(visited) > 0;
    }

    public VHost getHostData(){
        VHost h = new VHost();
        h.setVisitedNum(getVisitedNum());
        h.setQuestionNum(questionService.count());
        h.setUserNum(userService.count());

        return h;
    }
}
